package repository;

import java.io.File;
import java.io.IOException;
import fileoperation.FileCreation;

/**
 * Todo: Add your own code. JitInit.init("worktree") should be able to create a repository "worktree/.jit" ,
 *       which contains all the default files and other repositories inside. If the repository has
 *       already exists, delete the former one and create a new one. You're welcome to reconstruct the code,
 *       but don't change the code in the core directory.
 */
public class Repository {
    private static String workTree;	//working directory
    private static String jitDir;	//jit repository path


    /**
     * Constructor
     */
    public Repository() throws IOException {
        if(jitDir == ""){
            throw new IOException("The repository does not exist!");
        }
    }
    
    /**
     * Construct a new repository instance with certain path.
     * Constructor
     * @param path
     * @throws IOException
     */
    public Repository(String path) throws IOException {
        this.workTree = path;
        this.jitDir = path + File.separator + ".jit";
    }

    public static String getJitDir() {
        return jitDir;
    }

    public static String getWorkTree() {
        return workTree;
    }
    
    /**
     * Helper functions.
     * @return
     */
    public boolean exist(){ return new File(jitDir).exists(); }

    public boolean isFile(){ return new File(jitDir).isFile(); }

    public boolean isDirectory(){ return new File(jitDir).isDirectory(); }


    /**
     * Create the repository and files and directories inside.
     * @return boolean
     * @throws IOException
     */
    public void createRepo() throws IOException {
    /* Todo：Add your code here. */
        FileCreation.createDirectory(workTree,".jit");
        FileCreation.createFile(getJitDir(),"HEAD","ref: refs/heads/main");
        FileCreation.createFile(getJitDir(),"config","");
        FileCreation.createDirectory(getJitDir(),"hooks");
        FileCreation.createDirectory(getJitDir(),"logs");
        FileCreation.createDirectory(getJitDir(),"refs");
        FileCreation.createDirectory(getJitDir(),"branches");
        FileCreation.createFile(getJitDir(),"description","");
        FileCreation.createDirectory(getJitDir(),"info");
        FileCreation.createDirectory(getJitDir(),"objects");
    }

}
